package com.njupt.service.impl;

import com.njupt.common.pojo.EasyUITreeNode;
import com.njupt.mapper.TbItemCatMapper;
import com.njupt.pojo.TbItemCat;
import com.njupt.pojo.TbItemCatExample;
import com.njupt.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: XJL
 * @Description:查询商品分类信息
 * @Date: Create in 17:01 2018/5/27
 * @Modified By:
 **/
@Service
public class ItemCatServiceImpl implements ItemCatService {
    @Autowired
    private TbItemCatMapper tbItemCatMapper;


    @Override
    public List<EasyUITreeNode> getCatList(long parentId) {
        TbItemCatExample example = new TbItemCatExample();

        TbItemCatExample.Criteria criteria =example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        List<TbItemCat> list = tbItemCatMapper.selectByExample(example);
        List<EasyUITreeNode> resultList = new ArrayList<>();
        if (list!=null||list.size()>0){
            for (TbItemCat tbItemCat:list
                    ) {
                EasyUITreeNode treeNode = new EasyUITreeNode();
                treeNode.setId(tbItemCat.getId());
                //如果节点下有子节点state为closed,如果没有子节点state为open
                treeNode.setState(tbItemCat.getIsParent()?"closed":"open");
                treeNode.setText(tbItemCat.getName());
                resultList.add(treeNode);
            }
        }

        return resultList;
    }
}
